﻿using Aspose.Pdf.Facades;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace PDFAddWaterMark
{
    class Program
    {
        static void Main(string[] args)
        {
            //需要把File文件夹下的文件拷贝至Bin目录下


            string openFilePath = AppDomain.CurrentDomain.BaseDirectory + "C_035W24366-001-1-B.pdf";//需要添加水印的文件路径
            string saveFilePath = AppDomain.CurrentDomain.BaseDirectory + "035W24366-001-1-B.pdf";//水印添加完成后保存路径
            string imgFilePath = AppDomain.CurrentDomain.BaseDirectory + "035W24366-001-1-B.pdf.jpg";//水印图片路径

            #region ITextsharp方式
            PDFHelper.AddImageWatermarkPDF(openFilePath, saveFilePath, imgFilePath, 0.0F, 10);//添加水印图片
            //PDFHelper.AddWordWatermark2PDF(openFilePath, saveFilePath, saveFilePath);//添加水印文字
            //PDFHelper.setWatermark(openFilePath, saveFilePath, saveFilePath, "", "", 0);//添加水印文字

            //网址
            //https://www.cnblogs.com/haofaner/p/8779840.html 
            //https://github.com/itext/itextsharp 
            //https://www.cnblogs.com/jean69/p/6197740.html 
            //https://www.e-iceblue.cn/
            #endregion

            if (true)
            {
                return;
            }

            #region Aspose方式
            ///加入LIC
            Aspose.Pdf.License lic = new Aspose.Pdf.License();
            string licPath = AppDomain.CurrentDomain.BaseDirectory + "Aspose.Total.lic";
            lic.SetLicense(licPath);
            openFilePath = AppDomain.CurrentDomain.BaseDirectory + "C_035W24366-001-1-B.pdf";
            saveFilePath = AppDomain.CurrentDomain.BaseDirectory + "035W24366-001-1-B.pdf";
            imgFilePath = AppDomain.CurrentDomain.BaseDirectory + "035W24366-001-1-B.pdf.jpg";
            PdfFileStamp fileStamp = new PdfFileStamp(openFilePath, saveFilePath);
            Stamp stamp = new Stamp();
            stamp.BindImage(imgFilePath);
            float x = 0.0F;
            int h = 58;
            float y = h;//(1000 - imgHeight);
            ///位置
            stamp.SetOrigin(x, y);
            stamp.IsBackground = true;
            ///水印强度
            stamp.Opacity = 0.3F;
            try
            {
                fileStamp.AddStamp(stamp);
                fileStamp.Close();
            }
            catch
            {
                File.Copy(openFilePath, saveFilePath);
            }
            #endregion
        }
    }
}
